package ynu.edu.controller;

import jakarta.annotation.Resource;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;
import ynu.edu.config.DynamicConfig;
import ynu.edu.entity.User;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
@RefreshScope  // 配置更新域
public class UserController {
    
    @Resource
    private DynamicConfig dynamicConfig;
    
    // 模拟数据库存储
    private static Map<Integer, User> userDatabase = new HashMap<>();
    
    static {
        // 初始化一些测试数据
        User user1 = new User(1, "张三 from 11000", "123456");
        User user2 = new User(2, "李四 from 11000", "654321");
        userDatabase.put(1, user1);
        userDatabase.put(2, user2);
    }
    
    /**
     * GET方法：根据ID获取用户信息
     */
    @GetMapping("/getUserById/{userId}")
    public User GetUserById(@PathVariable("userId") Integer userId){
        User user = userDatabase.get(userId);
        if (user != null) {
            return user;
        } else {
            // 如果用户不存在，创建一个新用户返回
            User newUser = new User();
            newUser.setUserId(userId);
            newUser.setUserName("新用户 from 11000");
            newUser.setPassWord("123456");
            return newUser;
        }
    }
    
    /**
     * POST方法：创建新用户
     */
    @PostMapping("/createUser")
    public Map<String, Object> createUser(@RequestBody User user) {
        Map<String, Object> result = new HashMap<>();
        
        if (userDatabase.containsKey(user.getUserId())) {
            result.put("success", false);
            result.put("message", "用户ID " + user.getUserId() + " 已存在 (from 11000)");
            result.put("data", null);
        } else {
            // 设置服务标识
            user.setUserName(user.getUserName() + " from 11000");
            userDatabase.put(user.getUserId(), user);
            
            result.put("success", true);
            result.put("message", "用户创建成功 (from 11000)");
            result.put("data", user);
        }
        
        return result;
    }
    
    /**
     * PUT方法：更新用户信息
     */
    @PutMapping("/updateUser/{userId}")
    public Map<String, Object> updateUser(@PathVariable("userId") Integer userId, @RequestBody User user) {
        Map<String, Object> result = new HashMap<>();
        
        if (userDatabase.containsKey(userId)) {
            user.setUserId(userId);
            user.setUserName(user.getUserName() + " from 11000");
            userDatabase.put(userId, user);
            
            result.put("success", true);
            result.put("message", "用户更新成功 (from 11000)");
            result.put("data", user);
        } else {
            result.put("success", false);
            result.put("message", "用户ID " + userId + " 不存在 (from 11000)");
            result.put("data", null);
        }
        
        return result;
    }
    
    /**
     * DELETE方法：删除用户
     */
    @DeleteMapping("/deleteUser/{userId}")
    public Map<String, Object> deleteUser(@PathVariable("userId") Integer userId) {
        Map<String, Object> result = new HashMap<>();
        
        if (userDatabase.containsKey(userId)) {
            User deletedUser = userDatabase.remove(userId);
            result.put("success", true);
            result.put("message", "用户删除成功 (from 11000)");
            result.put("data", deletedUser);
        } else {
            result.put("success", false);
            result.put("message", "用户ID " + userId + " 不存在 (from 11000)");
            result.put("data", null);
        }
        
        return result;
    }
    
    /**
     * GET方法：获取所有用户列表
     */
    @GetMapping("/getAllUsers")
    public Map<String, Object> getAllUsers() {
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        result.put("message", "获取用户列表成功 (from 11000)");
        result.put("data", userDatabase.values());
        result.put("total", userDatabase.size());
        return result;
    }
    
    /**
     * 测试动态配置刷新
     */
    @GetMapping("/config")
    public Map<String, Object> getConfig() {
        Map<String, Object> result = new HashMap<>();
        result.put("message", dynamicConfig.getMessage());
        result.put("version", dynamicConfig.getVersion());
        result.put("environment", dynamicConfig.getEnvironment());
        result.put("server", "Provider-11000");
        result.put("timestamp", java.time.LocalDateTime.now());
        return result;
    }
}
